package com.example.demo.controller.jpa;
import com.example.demo.dao.UserRepository;
import com.example.demo.entity.dto.UserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Optional;

@RestController
public class JpaUserController {

    @Autowired
    UserRepository userRepository;

    @RequestMapping("/getUserInformation")
    public Optional<UserDTO> getUserInformation(Long personId){
        return userRepository.getUserInformation(personId);
    }

    @RequestMapping("/getUserInformationList")
    @Transactional
    public Page<UserDTO> getUserInformationList(){
        //分页选项
        PageRequest pageRequest = PageRequest.of(0, 3, Sort.Direction.DESC, "age");
        Page<UserDTO> userInformationList = userRepository.getUserInformationList(pageRequest);
        //查询结果总数
        System.out.println(userInformationList.getTotalElements());// 6
        //按照当前分页大小，总页数
        System.out.println(userInformationList.getTotalPages());// 2
        System.out.println(userInformationList.getContent());
        return userInformationList;
    }
}
